$(function(){
    //个人中心修改密码
    $('#jsResetPwdBtn').click(function(){

        userReadPassword();
    });
    //修改头像插件
    new uploadPreview({
        ImgShow:'avatarShow',
        UpBtn: 'avatarUp',
        DivShow: 'avatardiv',
        callback: function(){
            $('#jsAvatarForm').submit();
        }
    });

     //layDate日历插件
    laydate.render({
        elem: '#birth_day',
        format:'yyyy年MM月dd日',
        max: 0
    });

    //邮箱验证码
    $('#jsChangeEmailCodeBtn').on('click', function(){
        snedCodeChangeEmail();
    });

    //修改邮箱
    $('#jsChangeEmailBtn').on('click', function(){
        changeEmailSubmit();
    });

    //保存个人资料
    $('.heibtn>a').on('click', function(){
        keepingPersonaData();
    })
    //重置错误信息
    $('input[type=text]').focus(function(){
       $('#jsReadPwdTip').hide().html('');
       $('#jsChangeEmailTips').hide().html('');
       $('.error-tips').hide().html('');
    })
});

//保存个人资料
function keepingPersonaData(){

    $.ajax({
        cache: false,
        type: 'post',
        dataType: 'json',
        url: '/user/info/',
        data: $('#jsEditUserForm').serialize(),
        async: true,
        beforeSend: function(xhr,XMLHttpRequest) {
            $('#jsEditUserBtn').val('保存中...');
            $('#jsEditUserBtn').attr({disabled: 'disabled'});
        },
        success: function (data){
            if(data.status == "success"){$('#save_situation').text('保存成功');
                $('#save_information').text('个人信息修改成功');
                $('#icon').attr('src','/images/check2.png')
                setTimeout(function(){window.location.href = window.location.href;},1500);
            }else{
                if(data.nick_name)
                    $('#nick_name').parent('.error-tips').show().text('昵称'+data.nick_name);
                if(data.birday)
                    $('#birth_day').parent('.error-tips').show().text('生日'+data.birday);
                if(data.address)
                    $('#address').parent('.error-tips').show().text('地址'+data.address);
                $('#save_situation').text('保存失败');
                $('#save_information').text('个人信息更新失败');
                $('#icon').attr('src','/images/check4.png')
            }
        },
        complete: function(XMLHttpRequest){
            $('#jsEditUserBtn').val('保存');
            $('#jsEditUserBtn').removeAttr('disabled');
        }
    });
}

//个人中心修改密码
function userReadPassword(){
    var $btn=$('#jsResetPwdBtn');
    var $tips=$('#jsReadPwdTip');
     $.ajax({
         cache:false,
         type: 'POST',
         dataType: 'json',
         url: '/user/update_pwd/',
         data: $('#jsResePwdForm').serialize(),
         async:true,
         success: function(data){
             if(data.password1){
                 $tips.show().html('新密码，'+data.password1);
             }else if(data.password2){
                 $tips.show().html('确认密码，'+data.password2);
             }else if(data.status == 'success'){
                 $btn.val('提交成功...');
                 setTimeout(function(){
                    location.reload(); //刷新页面
                },3000);
             }else if(data.msg){
                 $tips.show().text(data.msg);
             }
         }
     })
}

//修改个人中心邮箱验证码
function snedCodeChangeEmail(){
    var $tips=$('#jsChangeEmailTips');
    var $btn=$('#jsChangeEmailCodeBtn');
    $.ajax({
        cache: false,
        type: 'GET',
        dataType: 'json',
        url: '/user/sendemail_code',
        data: $('#jsChangeEmailForm').serialize(),
        async: true,
        beforeSend: function(XMLHttpRequest){
            $btn.attr({disabled: 'disabled'});
            $btn.val('发送中...');
        },
        success: function(data){
            if(data.email){
                $tips,show().html(data.email);
            }else if(data.status == 'success'){
                $tips.show().html('邮箱验证码已发送!');
            }else if(data.status == 'failure'){
                $tips.show().html('邮箱验证码发送失败!');
            }
        },
        complete: function(XMLHttpRequest){
            $btn.val('获取验证码');
            $btn.removeAttr('disabled');
        }
    });
}

//邮箱修改
function changeEmailSubmit(){
    var $tips=$('#jsChangeEmailTips');
    var $btn=$('#jsChangeEmailBtn');
    $.ajax({
        cache: false,
        type: 'POST',
        dataType: 'json',
        url: '/user/update_email/',
        data: $('#jsChangeEmailForm').serialize(),
        async: true,
        beforeSend: function(XMLHttpRequest){
            console.log($('#jsChangeEmailForm').serialize());
            $('#jsChangeEmailCodeBtn').attr({disabled: "disabled" });
            $btn.val('验证中...').show(3000);
        },
        success: function(data){
            if(data.email){
                $tips.show().html(data.email);
            }else if(data.status == 'success'){
                $tips.show().html('邮箱信息更新成功!');
                setTimeout(function(){
                    location.reload();
                },3000);
            }else if(data.status == '验证码无效'){
                $tips.show().html('邮箱信息更新失败!');
            }
            btn.removeAttr('disabled');
        },
        complete: function(XMLHttpRequest){
            $btn.val('提交');
            $btn.attr('disabled');
        }
    })
}

